<template>
  <div id="main">
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <h2>登录界面</h2>

      <el-form-item label="用户名" prop="account">
        <el-input type="text" v-model="ruleForm2.account" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pword">
        <el-input type="password" v-model="ruleForm2.pword" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">登录</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
        <el-button @click="aa">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Message } from 'element-ui';
export default {
  name:'login',
  data() {
    return {
      ruleForm2: {
        account: 'hxh3306',
        pword: 'hxh2208',
      },
      rules2: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 24, message: '账号长度在6-24位之间', trigger: 'blur' },
        ],
        pword: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 24, message: '账号长度在6-24位之间', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      
      let account = this.ruleForm2.account
      let pword = this.ruleForm2.pword

      this.$axios.get(`http://localhost:8080/api/login?account=${account}&&pword=${pword}`).then(res =>{
        if(res.data[1] == true){
          Message.success('登录成功！！！')
          // 将用户名字存入sessionStorage 主页展示使用
          sessionStorage.setItem('uname',res.data[2].uname)
          // 将成功登录的管理员信息存入sessionStorage，等待调用
          sessionStorage.setItem('obtainAdminInformation',JSON.stringify(res.data[2]))
          // 存入token
          sessionStorage.setItem('token',res.data[0])

          this.$router.replace('./home')
        }else{
          Message.error('用户名或密码错误！！请检查')
        }
      },err => {
        console.log('请求出错！！！')
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    aa() {
      console.log(this)
    }
  }
}
</script>

<style lang="less" scoped>
#main {
  height: 100vh;
  position: relative;
  background-image: url(../assets/images/bg.a35a0ecc.jpg);

  .el-form {
    width: 450px;
    position: fixed;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
    background-color: aliceblue;
    padding: 20px;


    h2 {
      text-align: center;
      margin-bottom: 20px;
    }
  }
}
</style>
  